package com.crack.nga2.fragment;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

import com.crack.nga2.R;
import com.crack.nga2.entity.UploadImage;
import com.crack.nga2.localimage.LocalImageActivity;
import com.crack.nga2.localimage.NativeImageLoader;


import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class PostImageFragment extends Fragment implements AdapterView.OnItemClickListener {
    private static final int OK = 1;

    private ImageAdapter adapter = new ImageAdapter();
    private List<UploadImage> bitmaps = new ArrayList<>();
    private NativeImageLoader imageLoader;

    public PostImageFragment() {
        bitmaps.add(new UploadImage());
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_post_image, container, false);
        GridView gridView = (GridView) rootView.findViewById(R.id.gridView);
        gridView.setOnItemClickListener(this);
        gridView.setAdapter(adapter);
        imageLoader = NativeImageLoader.getInstance();
        return rootView;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (position == 0) {
            Intent intent = new Intent(getActivity(), LocalImageActivity.class);
            startActivityForResult(intent, OK);
        }
    }

    @SuppressWarnings("unchecked")
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == OK && resultCode == 1) {
            List<String> list = (List<String>) data.getSerializableExtra("images");
            for (String string : list) {
                UploadImage uploadImage = new UploadImage();
                uploadImage.setLocalPath(string);
                if (bitmaps.contains(uploadImage)) {
                    continue;
                }
                bitmaps.add(uploadImage);
            }
            adapter.notifyDataSetChanged();
        }
    }

    private class ImageAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return bitmaps.size();
        }

        @Override
        public Object getItem(int position) {
            return bitmaps.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            final ImageView imageView;
            if (view == null) {
                view = View.inflate(getContext(), R.layout.fragment_post_image_item, null);
            }
            final UploadImage uploadImage = bitmaps.get(position);
            imageView = (ImageView) view;
            imageView.setTag(uploadImage);
            String path = uploadImage.getLocalPath();
            if (path != null) {
                Bitmap bitmap = imageLoader.loadNativeImage(path, new Point(80, 80),
                        new NativeImageLoader.NativeImageCallBack() {
                            @Override
                            public void onImageLoader(Bitmap bitmap, String path) {
                                if (uploadImage == imageView.getTag() && bitmap != null) {
                                    imageView.setImageBitmap(bitmap);
                                }
                            }
                        });
                if (bitmap != null) {
                    imageView.setImageBitmap(bitmap);
                }
            }
            return view;
        }
    }


}
